﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Service;
using LevenBlog.Core.User;
using LevenBlog.Core.Data;

namespace LevenBlog.Core.WcfService {
    public abstract class BaseService : IDisposable {
        private IDbProvider provider = null;

        private IMembership membership = null;

        protected readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(BaseService));

        public BaseService() {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
        }

        protected IDbProvider Provider {
            get {
                if (provider == null) {
                    provider = ProviderManager.Factory.CreateDbProvider();
                    provider.Open();
                }
                return provider;
            }
        }

        public virtual IMembership Membership {
            get {
                if (membership == null) {
                    membership = new UserMembership(Provider);
                    membership.Read();
                }
                return membership;
            }
        }

        public virtual lb_config Config {
            get {
                return LevenBlog.Core.Service.Config.Get(Provider);
            }
        }

        public virtual lb_state State {
            get {
                return LevenBlog.Core.Service.State.Get(Provider);
            }
        }

        protected bool TryDecryptUserToken(string token, out lb_user user) {
            bool result = true;
            try {
                user = SecurityCookie.DecryptFromBase64(token);
            } catch {
                user = null;
                result = false;
            }
            return result;
        }

        protected string EncryptUserToken(lb_user user) {
            try {
                return SecurityCookie.EncryptToBase64(user);
            } catch {
                return String.Empty;
            }
        }

        protected Authority GetAuthority(lb_user user) {
            lb_usertype type;
            if (user.usertype == null) {
                type = UserType.Get(Provider, user.typeid);
            } else {
                type = user.usertype;
            }
            return GetAuthority(type);
        }

        protected Authority GetAuthority(string token) {
            lb_user user = null;
            if (TryDecryptUserToken(token, out user)) {
                return GetAuthority(user);
            } else {
                return GetAuthority(UserType.GetDefault(Provider));
            }
        }

        protected Authority GetAuthority(lb_usertype type) {
            Authority authority = new Authority();
            try {
                authority.Fill(type.competence);
            } catch {
                authority.Fill(UserType.GetDefault(Provider).competence);
            }
            return authority;
        }

        public void Dispose() {
            if (provider != null)
                provider.Dispose();
        }
    }
}
